package jml.defaultimple;

import java.util.ArrayList;
import java.util.List;

import jml.def.Filter;
import jml.def.Label;
import jml.def.Library;
import jml.defaultimple.util.JMLLoader;

public class FilterImple implements Filter
{
	private String name;
	private boolean alwaysShown;
	private int ifNotSpecified;
	private String sortString;
	private ArrayList<Label> labels;
	private ArrayList<String> excludedLabels;
	
	
	public FilterImple()
	{
		labels = new ArrayList<Label>();
		excludedLabels = new ArrayList<String>();
	}
	
	public void excludeLabel(String label)
	{
		excludedLabels.add(label);
	}
	
	public void addLabel(Label lbl)
	{
		labels.add(lbl);
	}
	
	public void setName(String name)
	{
		this.name=name;
	}
	
	@Override
	public String getName()
	{
		return name;
	}

	public void setAlwaysShown(boolean b)
	{
		this.alwaysShown=b;
	}
	
	@Override
	public boolean isAlwaysShown()
	{
		return alwaysShown;
	}
	
	public void setIfNotSpecified(int s)
	{
		this.ifNotSpecified=s;
	}
	
	@Override
	public int getIfNotSpecified()
	{
		return ifNotSpecified;
	}

	public void setSort(String s)
	{
		this.sortString = s;
	}
	
	@Override
	public String getSort()
	{
		return sortString;
	}
	
	@Override
	public Label[] getLabels()
	{
		ArrayList<Label> finalLabels = new ArrayList<Label>();
		for(Label label:labels)
		{
			if( !excludedLabels.contains(label.getName()) )
			{
				finalLabels.add(label);
			}
		}
		
		Label[] ret = new Label[finalLabels.size()];
		return finalLabels.toArray(ret);		
	}

	@Override
	public Label getLabel(String labelname)
	{
		for(Label lbl:labels)
		{
			if( lbl.getName().equals(labelname) )
			{
				return lbl;
			}
		}
		
		return null;
	}

	@Override
	public String toString()
	{
		String s = "Filter: [name=" + name + ", alwaysShown=" + alwaysShown + ", sortString=" + sortString + ", ifNotSpecified=" + ifNotSpecified + "]\n";

//		s+="Labels[\n";
//		for(Label label:labels)
//		{
//			s+=label.toString();
//		}
//		s+="]\n";
		
		return s;
	}
	
	public static void main(String[] args)
	{
		
		Library library = JMLLoader.load("WebContent/jmusiclibrary.cfg.xml");
		
		Filter f = library.getFilter("Genre");
		
		for(Label lbl:f.getLabels())
		{
			List<String> sub = lbl.getSublabels();
			
			for(String s:sub)
			{
				Label x = f.getLabel(s);
			}
		}
		
	}

	public List<Label> getListLabels()
	{
		return labels;
	}

	@Override
	public int countTitles(String labelName, boolean includingSublabels)
	{
		Label label = getLabel(labelName);
		
		if( label==null )
		{
			return 0;
		}
		
		if( !includingSublabels )
		{
			return label.getTitles().size();
		}
		
		int n=label.getTitles().size();
		List<String> sublabels = label.getSublabels();
		
		for(int i=0; i<sublabels.size(); i++)
		{
			label = getLabel(sublabels.get(i));
			if( label!=null )
			{
				n+=label.getTitles().size();
			}
		}
		
		return n;
	}
	
	
	
}
